__CreateJSPath = function (js) {
    var scripts = document.getElementsByTagName("script");
    var path = "";
    for (var i = 0, l = scripts.length; i < l; i++) {
        var src = scripts[i].src;
        if (src.indexOf(js) != -1) {
            var ss = src.split(js);
            path = ss[0];
            break;
        }
    }
    var href = location.href;
    href = href.split("#")[0];
    href = href.split("?")[0];
    var ss = href.split("/");
    ss.length = ss.length - 1;
    href = ss.join("/");
    if (path.indexOf("http:") == -1 && path.indexOf("file:") == -1) {
        path = href + "/" + path;
    }
    return path;
}
var bootPATH = __CreateJSPath("index.js");
document.write('<script type="text/javascript" src="'+bootPATH+'../dataSource.js"></script>');
document.write('<script type="text/javascript" src="'+bootPATH+'/submitComment.js"></script>');


// 全局变量
var $userCurrentCaseId = 0;
var $currentCasePageNumber = 1;	//它控制了留言版的当前页数，先改变页码，再加载数据
var $casePageSize = 8;
var $currentCommentPageNumber = 1;	//它控制了往期回顾的当前页数，先改变页码，再加载数据
var $commentPageSize = 4;

var $caseTotalCount = 0;
var $commentTotalCount = 0;



//加载最后发布的案例信息
function loadCaseInfo(index) {
	// index = 2, 代表加载按日期排序后的第二个案例
	$.post("LoadCaseInfo", {index : index, pageNumber : $currentCasePageNumber}, function(text) {
		var data = (new Function("return "+text))();
		if (data.status.indexOf('成功')) {
			$userCurrentCaseId = data.caseId;
			$("#currentCase").text(data.caseName);
			$("#currentCaseContent").text(data.content);
			
			// 加载第一页留言
			loadUserComment($userCurrentCaseId, 1);
		}
	});
}

//加载留言列表
function loadUserComment(caseId, pageNumber) {
	$.post("LoadUserComment", { caseId : caseId, pageNumber : pageNumber, pageSize: $commentPageSize}, function(text) {
		var data = (new Function("return " + text))();
		if(data.total > 0) {
			$commentTotalCount = data.total;
			
			// 加载留言信息
			var list = data.rows;
			var htmlItem = [];
			for(var i = 0; i < list.length; i++) {
				htmlItem.push("<li class='list-group-item'>" + 
									"<span class='badge'>" + list[i].guestName + "</span>" +
									"<div class='well-sm'><p>" + list[i].content + "</p></div>"  + 
									"<span class='well-sm'>" + list[i].commentDate + "</span>" + 
									"</li>");
			}
			$("#commentListGroup").html(htmlItem);
			
			// 控制页码显示
			var htmlPageItem = [];
			$currentCommentPageNumber = pageNumber;
			//alert($currentCommentPageNumber);
			// 首页和第一页的逻辑
			if(pageNumber == 1) {
				htmlPageItem.push("<li class='disabled' onClick=\"loadCommentPage('Previous')\"><a href='#'>上一页</a></li>");
				htmlPageItem.push("<li class='active' onClick='loadCommentPage(1)'><a href='#'>1</a></li>");
			} else {
				htmlPageItem.push("<li onClick=\"loadCommentPage('Previous')\"><a href='#'>上一页</a></li>");
				htmlPageItem.push("<li onClick='loadCommentPage(1)'><a href='#'>1</a></li>");
			}
			// 中间页码的逻辑
			for(var j = 2; j <= Math.ceil($commentTotalCount / $commentPageSize); j++) {
				//alert(j);
				if(pageNumber == j) {
					htmlPageItem.push("<li class='active' onClick='loadCommentPage(" + j + ")'><a href='#'>" + j + "</a></li>");
				} else {
					htmlPageItem.push("<li onClick='loadCommentPage(" + j + ")'><a href='#'>" + j + "</a></li>");
				}
			}
			// 最后一页的控制逻辑
			if(pageNumber == Math.ceil($commentTotalCount / $commentPageSize))
				htmlPageItem.push("<li class='disabled' onClick=\"loadCommentPage('Next')\"><a href='#'>下一页</a></li>");
			else
				htmlPageItem.push("<li onClick=\"loadCommentPage('Next')\"><a href='#'>下一页</a></li>");
			
			$("#commentPagination").html(htmlPageItem);
			
		}
		else {
			$("#commentListGroup").html("");
			$("#commentPagination").html("");
		}
	});
}

//加载往期回顾列表
function loadCaseList(pageNumber) {
	$.post("LoadCaseList", { pageNumber : pageNumber, pageSize : $casePageSize}, function(text) {
		var data = (new Function("return " + text))();
		$caseTotalCount = data.total;
		
		// 加载案例信息
		var list = data.rows;
		var htmlItem = [];
		htmlItem.push("<li class='list-group-item active'>往期回顾</li>");
		for(var i = 0; i < list.length; i++) {
			htmlItem.push("<li class='list-group-item'>" + 
								"<span class='badge'>" + list[i].publishDate + "</span>" +
								"<div><a href='#' onclick='loadCaseInfo("+ (i+1) +")'>" + list[i].caseName + "</a></div>"  + 
								"</li>");
		}
		$("#caseListGroup").html(htmlItem);
		
		// 控制页码显示
		var htmlPageItem = [];
		$currentCasePageNumber = pageNumber;
		//alert($currentCommentPageNumber);
		// 首页和第一页的逻辑
		if(pageNumber == 1) {
			htmlPageItem.push("<li class='disabled' onClick=\"loadCasePage('Previous')\"><a href='#'>上一页</a></li>");
			htmlPageItem.push("<li class='active' onClick='loadCasePage(1)'><a href='#'>1</a></li>");
		} else {
			htmlPageItem.push("<li onClick=\"loadCasePage('Previous')\"><a href='#'>上一页</a></li>");
			htmlPageItem.push("<li onClick='loadCasePage(1)'><a href='#'>1</a></li>");
		}
		// 中间页码的逻辑
		for(var j = 2; j <= Math.ceil($caseTotalCount / $casePageSize); j++) {
			//alert(j);
			if(pageNumber == j) {
				htmlPageItem.push("<li class='active' onClick='loadCasePage(" + j + ")'><a href='#'>" + j + "</a></li>");
			} else {
				htmlPageItem.push("<li onClick='loadCasePage(" + j + ")'><a href='#'>" + j + "</a></li>");
			}
		}
		// 最后一页的控制逻辑
		if(pageNumber == Math.ceil($caseTotalCount / $casePageSize))
			htmlPageItem.push("<li class='disabled' onClick=\"loadCasePage('Next')\"><a href='#'>下一页</a></li>");
		else
			htmlPageItem.push("<li onClick=\"loadCasePage('Next')\"><a href='#'>下一页</a></li>");
		
		$("#casePagination").html(htmlPageItem);
		loadCaseInfo(1);
		
	});
}


//加载新的留言页
function loadCommentPage(pageNumber) {
	if(pageNumber == "Previous") {
		pageNumber = $currentCommentPageNumber - 1;
	} else if(pageNumber == "Next") {
		pageNumber = $currentCommentPageNumber + 1;
	}
	
	loadUserComment($userCurrentCaseId, pageNumber);
}

//加载新的案例回顾页
function loadCasePage(pageNumber) {
	if(pageNumber == "Previous") {
		pageNumber = $currentCasePageNumber - 1;
	} else if(pageNumber == "Next") {
		pageNumber = $currentCasePageNumber + 1;
	}
	
	loadCaseList(pageNumber);
}


$(document).ready(function(){
	loadCaseList(1);
	loadCaseInfo(1);
});
